﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Xml;
using System.Collections;

namespace Fawcett.Plugins.Arduino
{
    public class ChannelHandler : IConfigurationSectionHandler
    {
        public ChannelHandler()
        {
        }
 
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
                return Channel.Map(section);
        }
     }


    public class Channel : IChannel
    {
        public string Name { get; set; }
        public string Board { get; set; }
        public string Pin { get; set; }
        public string Type { get; set; }

        static public ChannelCollection Map(System.Xml.XmlNode section)
        {
            ChannelCollection maps = new ChannelCollection();

            foreach (XmlNode node in section.ChildNodes)
            {
                try
                {
                    maps.Add(new Channel()
                    {
                        Name = node.Attributes["Name"].Value,
                        Board = node.Attributes["Board"].Value,
                        Pin = node.Attributes["Pin"].Value,
                        Type = node.Attributes["Type"].Value
                    });

                }
                catch (Exception e)
                {
                    string str = String.Format(Fawcett.Plugins.Properties.Resources.ERROR_3, section.Name);
                    Fawcett.Plugins.MessageBox.Show(e, str);
                }
            }

            return maps;
        }
    }

    public interface IChannel
    {
         string Name { get; set; }
         string Board { get; set; }
         string Pin { get; set; }
         string Type { get; set; }
    }

    public class ChannelCollection : CollectionBase
    {
        public ChannelCollection()
        {
        }

        public ChannelCollection(ChannelCollection value)
        {
            this.AddRange(value);
        }

        public ChannelCollection(IChannel[] value)
        {
            this.AddRange(value);
        }


        public IChannel this[int index]
        {
            get { return ((IChannel)(this.List[index])); }
        }

        public int Add(IChannel value)
        {
            return this.List.Add(value);
        }


        public void AddRange(IChannel[] value)
        {
            for (int i = 0; (i < value.Length); i = (i + 1))
            {
                this.Add(value[i]);
            }
        }

        public void AddRange(ChannelCollection value)
        {
            for (int i = 0; (i < value.Count); i = (i + 1))
            {
                this.Add((IChannel)value.List[i]);
            }
        }

        public bool Contains(IChannel value)
        {
            return this.List.Contains(value);
        }

        public void CopyTo(IChannel[] array, int index)
        {
            this.List.CopyTo(array, index);
        }

        public IChannel[] ToArray()
        {
            IChannel[] array = new IChannel[this.Count];
            this.CopyTo(array, 0);

            return array;
        }

        public int IndexOf(IChannel value)
        {
            return this.List.IndexOf(value);
        }

        public void Insert(int index, IChannel value)
        {
            List.Insert(index, value);
        }

        public void Remove(IChannel value)
        {
            List.Remove(value);
        }

        public new ChannelCollectionEnumerator GetEnumerator()
        {
            return new ChannelCollectionEnumerator(this);
        }

        public class ChannelCollectionEnumerator : IEnumerator
        {
            private IEnumerator _enumerator;
            private IEnumerable _temp;

            public ChannelCollectionEnumerator(ChannelCollection mappings)
            {
                _temp = ((IEnumerable)(mappings));
                _enumerator = _temp.GetEnumerator();
            }

            public IChannel Current
            {
                get { return ((IChannel)(_enumerator.Current)); }
            }

            object IEnumerator.Current
            {
                get { return _enumerator.Current; }
            }

            public bool MoveNext()
            {
                return _enumerator.MoveNext();
            }

            bool IEnumerator.MoveNext()
            {
                return _enumerator.MoveNext();
            }

            public void Reset()
            {
                _enumerator.Reset();
            }

            void IEnumerator.Reset()
            {
                _enumerator.Reset();
            }
        }
    }

}
